<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jquery3.3.1.js"></script>
    <style>
        #box{width: 200px;height: 200px;background-color: red;position: absolute;left: 0;top: 0;}
    </style>
</head>
<body>
    <div id="box"></div>
    <!-- outerWidth属性是一只读的整数,声明了整个窗口的宽度,ie不支持这个属性 -->
    <script>
        $(function(){
            var ow=$(window).width();
            var oh=$(window).height();
            // console.log(ow,oh);

            function cfn(w,h){
                var aleft=(w-$("#box").outerWidth())/2;
                var atop=(h-$("#box").outerHeight())/2;
                $("#box").css({"left":aleft,"top":atop})
            }
            cfn(ow,oh)

            $(window).resize(function(){
                var ow=$(window).width();
                var oh=$(window).height();
                cfn(ow,oh)
            })
        })
    </script>
</body>
</html>